package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.BusinessExtrasType;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.ibiz.base.IDpBaseDataDictionaryService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.his.IHisYwBusinessBuyerService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessBuyerService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.yw.IYwFeesSubService;
import com.zjdiepu.www.ibiz.yw.IYwFundApplyService;
import com.zjdiepu.www.ibiz.zx.IZxCreditQueryService;
import com.zjdiepu.www.model.his.dto.HisYwBusinessBuyer;
import com.zjdiepu.www.model.yw.dto.YwBusinessBuyer;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.response.YwBusinessBuyerItem;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.model.zx.response.ZxCreditQueryDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 购车人历史信息Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月24日 14时43分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "HisYwBusinessBuyerViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"购车人历史信息"}, produces="text/html;charset=UTF-8")
public class HisYwBusinessBuyerViewController extends BaseController {
	@Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
	private IYwBusinessService ywBusinessService;
    @Resource
    private IYwFundApplyService ywFundApplyService;
    @Resource
    private IZxCreditQueryService zxCreditQueryService;
    @Resource
    private IDpBaseDataDictionaryService dBaseDataDictionaryService;
    @Resource
    private IYwFeesSubService ywFeesSubService;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    @Resource
    private IYwBusinessBuyerService ywBusinessBuyerService;
    @Resource
    private IHisYwBusinessBuyerService hisYwBusinessBuyerService;
    
    @ApiOperation(value = "修改购车人历史", response = Boolean.class, httpMethod="GET", tags={"修改购车人历史"})
    @RequestMapping(value = "hisYwBusinessBuyerDetail", method = RequestMethod.GET)
    public String hisYwBusinessBuyerDetail(Model model, String busiId, String hisId) {
    	if (!StringUtils.isNoneBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	viewScopeControllerHelper.preparedBusiness(busiId, model);
	    viewScopeControllerHelper.preparedNowTime(model);
		HisYwBusinessBuyer hisYwBusinessBuyer = hisYwBusinessBuyerService.selectById(hisId);
		model.addAttribute("hisYwBusinessBuyer", hisYwBusinessBuyer);
		
		if(null != hisYwBusinessBuyer) {
			String buyerId = hisYwBusinessBuyer.getBuyerId();
			if(StringUtils.isNotBlank(buyerId)) {
				YwBusinessBuyer ywBusinessBuyer = ywBusinessBuyerService.selectById(buyerId);
				model.addAttribute("ywBusinessBuyer", ywBusinessBuyer);
			}
		}
    	return "system/yw/hisYwBusinessBuyerDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: ywBussinessDetail  <br/>
     * @Description: 修改购车人信息 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改购车人信息", response = Boolean.class, httpMethod="GET", tags={"新增"})
    @RequestMapping(value = "ywBussinessDetail", method = RequestMethod.GET)
    public String ywBussinessDetail(Model model, String id) {
    	//下拉选项
    	model.addAttribute("maritalStatus", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.MARITAL_STATUS,"请选择..."));
    	model.addAttribute("dealerLevel", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.DEALER_LEVEL,"请选择..."));
    	model.addAttribute("educationType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.EDUCATION,"请选择..."));
    	model.addAttribute("sexType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.SEX,"请选择..."));
    	model.addAttribute("loanType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_TYPE,"请选择..."));
    	model.addAttribute("loanBankType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_BANK,"请选择..."));
    	model.addAttribute("licencePlateType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LICENCE_PLATE_TYPE,"请选择..."));
    	model.addAttribute("carType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_TYPE,"请选择..."));
    	model.addAttribute("carPlaceType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_PLACE,"请选择..."));
    	model.addAttribute("loanMonthType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_MONTH,"请选择..."));
    	model.addAttribute("jobType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.ZHI_WEI,"请选择..."));
    	model.addAttribute("workAgeType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.WORK_AGE,"请选择..."));
    	model.addAttribute("carStatus", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_STATUS,"请选择..."));
    	model.addAttribute("carPurpose", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_PURPOSE,"请选择..."));
    	model.addAttribute("channelType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CHANNEL_TYPE,"请选择..."));
    	model.addAttribute("channelStatus", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CHANNEL_STATUS,"请选择..."));
    	model.addAttribute("contactRelation", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CONTACT_RELATION,"请选择..."));
    	model.addAttribute("jobStatus", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.JOB_STATUS,"请选择..."));
    	model.addAttribute("affiliateStatus", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.AFFILIATE_STATUS,"请选择..."));
    	
    	model.addAttribute("CHU_SHEN_DAN_CHU_SHEN_ZHONG", BusiStatus.CHU_SHEN_DAN_CHU_SHEN_ZHONG.getCode());
    	model.addAttribute("QIAN_SHU_HE_TONG_ZHONG", BusiStatus.QIAN_SHU_HE_TONG_ZHONG.getCode());
    	
    	//判断条件
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	viewScopeControllerHelper.preparedRegin(model,null,null);
    	viewScopeControllerHelper.preparedCarModel(model);
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
		
    	YwBusinessExtras ywBusinessExtras_ = new YwBusinessExtras();
    	ywBusinessExtras_.setBusiId(id);
    	ywBusinessExtras_.setTypeExtras(BusinessExtrasType.PHONE_AUDIT.getCode());
    	ywBusinessExtras_.setCodeExtras(BusinessExtrasCodeType.IS_PHONE_AUDIT.getCode());
    	ywBusinessExtras_.setDpStatus(DpStatus.NORMAL.getCode());
    	YwBusinessExtras ywBusinessExtras = ywBusinessExtrasService.selectRefined(ywBusinessExtras_);
    	if(null != ywBusinessExtras) {
    		model.addAttribute("ifPhone", "1".equals(ywBusinessExtras.getValueExtras()));
    	}
    	
    	// 查询征信信息
		String creditApplyId = ywBusinessDetail.getQueryCreditApplyId();
		if (StringUtils.isNotBlank(creditApplyId)) {
			List<ZxCreditQueryDetail> zxCrediQueryDetails = zxCreditQueryService.selectMultiByApplyId(creditApplyId);
			Map<String, List<YwBusinessBuyerItem>> ywBusinessBuyerItems = new LinkedHashMap<String, List<YwBusinessBuyerItem>>();
			int i = 1;
			for (ZxCreditQueryDetail zxCrediQueryDetail : zxCrediQueryDetails) {
				String creditPeopleType = zxCrediQueryDetail.getCreditPeopleType();
				String cusNo = zxCrediQueryDetail.getCusNo();
				if("01".equals(creditPeopleType)) {
					model.addAttribute("PEI_OU_CUS_NO", cusNo);
				} else if("03".equals(creditPeopleType)) {
					model.addAttribute("CUS_NO", cusNo);
				} else {
					model.addAttribute("DAN_BAO_REN_CUS_NO_" + i, cusNo);
					i++;
				}
				List<YwBusinessBuyerItem> ywBusinessBuyerItems_ = ywBusinessBuyerItems.get(creditPeopleType);
				if(null == ywBusinessBuyerItems_) {
					ywBusinessBuyerItems_ = CollectionInitUtil.emptyList();
				}
				ywBusinessBuyerItems_.add(new YwBusinessBuyerItem(zxCrediQueryDetail.getCusName(), zxCrediQueryDetail.getCusNo()));
				ywBusinessBuyerItems.put(creditPeopleType, ywBusinessBuyerItems_);
			}
			model.addAttribute("ywBusinessBuyerItems", ywBusinessBuyerItems);
		}
    			
        return "system/yw/his_ywBussinessDetail";
    }
}
